/**
 * clock.c
 *
 * A GTK+ widget that implements a clock face
 *
 * (c) 2005, Davyd Madeley
 *
 * Authors:
 *   Davyd Madeley  <davyd@madeley.id.au>
 */

#include <gtk/gtk.h>
#include <math.h>

#include "clock.h"

G_DEFINE_TYPE (EggClockFace, egg_clock_face, GTK_TYPE_DRAWING_AREA);

static gboolean egg_clock_face_expose (GtkWidget *clock, GdkEventExpose *event);

static void
egg_clock_face_class_init (EggClockFaceClass *class)
{
	GtkWidgetClass *widget_class;

	widget_class = GTK_WIDGET_CLASS (class);

	widget_class->expose_event = egg_clock_face_expose;
}

static void
egg_clock_face_init (EggClockFace *clock)
{
}

static void
draw (GtkWidget *clock, cairo_t *cr)
{
	double x, y, xx, yy, X;
	double radius;
	int i;
	
	xx = clock->allocation.width;
	yy = clock->allocation.height;
	x = clock->allocation.x + clock->allocation.width / 2; 
 	y = clock->allocation.y + clock->allocation.height / 2;
 	radius = MIN (clock->allocation.width / 2, 
 		      clock->allocation.height / 2) - 5; 

/* 	/\* clock back *\/ */
/*  	cairo_arc (cr, x, y, radius, 0, 2 * M_PI); */
/*  	cairo_set_source_rgb (cr, 1, 1, 1);  */
/*  	cairo_fill_preserve (cr);  */
/*  	cairo_set_source_rgb (cr, 0, 0, 0);  */
/*  	cairo_stroke (cr);  */

	/* clock ticks */
/* 	for (i = 0; i < 12; i++) */
/* 	{ */
/* 		int inset; */
	
/* 		cairo_save (cr); /\* stack-pen-size *\/ */
		
/* 		if (i % 3 == 0) */
/* 		{ */
/* 			inset = 0.2 * radius; */
/* 		} */
/* 		else */
/* 		{ */
/* 			inset = 0.1 * radius; */
/* 			cairo_set_line_width (cr, 0.5 * */
/* 					cairo_get_line_width (cr)); */
/* 		} */
		
/* 		cairo_move_to (cr, */
/* 				x + (radius - inset) * cos (i * M_PI / 6), */
/* 				y + (radius - inset) * sin (i * M_PI / 6)); */
/* 		cairo_line_to (cr, */
/* 				x + radius * cos (i * M_PI / 6), */
/* 				y + radius * sin (i * M_PI / 6)); */
/* 		cairo_stroke (cr); */
/* 		cairo_restore (cr); /\* stack-pen-size *\/ */
/* 	} */

/* 	cairo_rectangle(cr, 10, 10, 20, 30); */
/* 	cairo_stroke (cr); */
/* 	cairo_restore (cr); */ /* stack-pen-size */
	/*
	  
	 cr un cairo contex
	  void        cairo_rel_move_to               (cairo_t *cr,
                                             double dx,
	                                   double dy);

	  void        cairo_move_to                   (cairo_t *cr,
                                             double x,
                                             double y);
	*/
/* 	cairo_set_line_width (cr, 20); */
	cairo_set_source_rgb (cr, 0, 0, 0);

/* 	cairo_rel_line_to(cr, 671,613); */
/* 	cairo_rel_curve_to(cr,720,577, 792,514, 792,397); */
/* 	cairo_rel_curve_to(cr,792.50000,325.65870,729.50000,280.65870,657.50000,280.65870); */
/* 	cairo_rel_curve_to(cr, 585.50000,280.65870, 504.50000,334.65870, 504.50000,334.65870); */
/* 	cairo_rel_curve_to(cr, 504.50000,334.65870, 549.50000,190.65870, 423.50000,154.65870); */
/* 	cairo_rel_line_to(cr,423.50000,118.65870); */
/* 	cairo_rel_line_to(cr,477.50000,118.65870); */
/* 	cairo_rel_line_to(cr,477.50000,64.658690); */
/* 	cairo_rel_line_to(cr,423.50000,64.658690); */
/* 	cairo_rel_line_to(cr,423.50000,10.658690); */
/* 	cairo_rel_line_to(cr,369.50000,10.658690); */
/* 	cairo_rel_line_to(cr,369.50000,64.658690); */
/* 	cairo_rel_line_to(cr,315.50000,64.658690); */
/* 	cairo_rel_line_to(cr,315.50000,118.65870); */
/* 	cairo_rel_line_to(cr,369.50000,118.65870); */
/* 	cairo_rel_line_to(cr,369.50000,154.65870); */

/* 	cairo_stroke (cr); */



/* 	cairo_move_to(cr, 0, 0); */
/*  	cairo_rel_move_to(cr, 0.10*xx,0.15*yy); */
/* 	cairo_rel_line_to(cr, 0.80*xx,0.15*yy); */
/* 	cairo_line_to(cr, 0.30*xx,0.45*yy); */
/* 	cairo_close_path(cr); */

	if ( xx > yy ){
		X = yy;
	} else {
		X =xx;
	}

	cairo_rectangle(cr, 5, 5, X*0.90, X*0.90);

/* 	cairo_curve_to(cr, 7+x,5+y, 10+x,15+y, 7+x,3+y); */
/* 	cairo_stroke (cr); */

/*  	cairo_move_to(cr, 0.10*x,0.95*y); */
/* 	cairo_curve_to(cr, 1+x,5+y, 20+x,15+y, 17+x,3+y); */
	cairo_stroke (cr);

	cairo_restore (cr);/*  stack-pen-size */

/* 	cairo_arc (cr, 128.0, 128.0, 76.8, 0, 2 * M_PI); */
/* 	cairo_clip (cr); */
	
/* 	cairo_new_path (cr);  /\* current path is not */
/* 				 consumed by cairo_clip() *\/ */
/* 	cairo_rectangle (cr, 0, 0, 256, 256); */
/* 	cairo_fill (cr); */
/* 	cairo_set_source_rgb (cr, 0, 1, 0); */
/* 	cairo_move_to (cr, 0, 0); */
/* 	cairo_line_to (cr, 256, 256); */
/* 	cairo_move_to (cr, 256, 0); */
/* 	cairo_line_to (cr, 0, 256); */
/* 	cairo_set_line_width (cr, 10.0); */
/* 	cairo_stroke (cr); */

	/*
C 243.50000,190.65870 288.50000,334.65870 288.50000,334.65870 
C 288.50000,334.65870 207.50000,280.65870 135.50000,280.65870 
C 63.500000,280.65870 0.50000000,325.65870 0.50000000,397.65870 
C 0.50000000,514.65870 72.500000,577.65870 126.50000,613.65870 
L 144.50000,730.65870 
C 153.50000,757.65870 216.50000,793.65870 396.50000,793.65870 
C 576.50000,793.65870 639.50000,757.65870 648.50000,730.65870 z M 396.50000,451.65870 
			   */
}

       
static gboolean
egg_clock_face_expose (GtkWidget *clock, GdkEventExpose *event)
{
	cairo_t *cr;

	/* get a cairo_t */
	cr = gdk_cairo_create (clock->window);

	cairo_rectangle (cr,
			event->area.x, event->area.y,
			event->area.width, event->area.height);
	cairo_clip (cr);
	
	draw (clock, cr);

	cairo_destroy (cr);

	return FALSE;
}

GtkWidget *
egg_clock_face_new (void)
{
	return g_object_new (EGG_TYPE_CLOCK_FACE, NULL);
}
